package model;

import java.awt.*;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Student
 * Date: 30/11/12
 * Time: 10:19
 * To change this template use File | Settings | File Templates.
 */
public class ConfigurableTeamState implements TeamState {

    private Backlog backlog = null;
    private Done done = null;
    private WorkInProgress workInProgress = null;

    private Color color;
    private String name;
    private List<Integer> dice;


    private int availableCapacity;

    public ConfigurableTeamState() {
        backlog = new Backlog();
        workInProgress = new WorkInProgress();
        done = new Done();
        this.name = "Green";
        this.color = Color.green;
    }


    public ConfigurableTeamState(String teamName, Color color) {
        this();
        this.name = teamName;
        this.color = color;
    }

    public void roll() {
        List<Integer> dice = new ArrayList<Integer>();
        setDice(dice);
        availableCapacity = 0;
        for (int i = 0; i < 5; i++) {
            int face = new DiceRollerImp().roll();
            if (face == 1) {
                availableCapacity++;
            }
            dice.add(face);

        }
    }

    @Override
    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public Color getColor() {
        return this.color;
    }

    public void setColor(Color color) {
        this.color = color;
    }

    @Override
    public LinkedList<Task> getBacklog() {
        return this.backlog.getTaskList();
    }

    @Override
    public Backlog getBacklogObject() {
        return this.backlog;
    }

    @Override
    public List<List<Task>> getProgress() {
        return this.workInProgress.getWorkTasksInProgress();
    }

    @Override
    public LinkedList<Task> getDone() {
        return this.done.getTaskList();
    }


    public List<Integer> getDice() {
        return dice;
    }

    public void setDice(List<Integer> dice) {
        this.dice = dice;
    }

    public WorkInProgress getWorkInProgress() {
        return this.workInProgress;
    }

    public void moveTask(Task task) {
        if (availableCapacity <= 0) {
            return;
        }
        if (this.backlog.getTaskList().contains(task)) {
            this.backlog.getTaskList().remove(task);
            this.workInProgress.addTask(task);
            availableCapacity--;
        } else if (this.workInProgress.getTaskList().contains(task)) {
            task.incrementUnitsOfWorkCompleted();
            availableCapacity--;
        }

        if (this.workInProgress.getFinishedTaskList().contains(task)) {
            this.workInProgress.getTaskList().remove(task);
            this.done.addTask(task);
            this.workInProgress.getFinishedTaskList().remove(task);
            availableCapacity--;
        }
    }

    public boolean decrementCapacity() {
        if (availableCapacity <= 0) {
            return false;
        }
        availableCapacity--;
        return true;
    }
}
